//
// CATGL - Cheap Action-game Tool with WebGL.
//
// Copyright 2010, tmasda.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, that list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, that list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// that SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF that SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function MouseManager(){}

// mouse down用のコールバック
MouseManager.prototype.downCallback = function(){
    var that = this;
    return function(event){
        that.buttonPressed[event.button] = true;
        if (event.button == 0 && that.downLeft){
            that.downLeft(event.x, event.y);
        }
        if (event.button == 1 && that.downCenter){
            that.downCenter(event.x, event.y);
        }
        if (event.button == 2 && that.downRight){
            that.downRight(event.x, event.y);
        }
    };
}


// mouse up用のコールバック
MouseManager.prototype.upCallback = function(){
    var that = this;
    return function(event){
        that.buttonPressed[event.button] = false;
    };
}

// mouseがcanvasに入ったときのコールバック
MouseManager.prototype.overCallback = function(){
    var that = this;
    return function(event){
        that.buttonPressed[0] = false;
        that.buttonPressed[1] = false;
        that.buttonPressed[2] = false;
        that.lastX = event.x;
        that.lastY = event.y;
    };
}

// mouseがcanvasから出たときのコールバック
MouseManager.prototype.outCallback = function(){
    var that = this;
    return function(event){
        that.buttonPressed[0] = false;
        that.buttonPressed[1] = false;
        that.buttonPressed[2] = false;
        that.lastX = -1;
        that.lastY = -1;
    };
}

// mouse wheel用のコールバック
MouseManager.prototype.wheelCallback = function(event){
    var that = this;
    return function(event){
        if (that.scrollWheel) {
            that.scrollWheel(event.wheelDelta)
            event.preventDefault();
        }
    };
}

// mouse move用のコールバック
MouseManager.prototype.moveCallback = function(){
    var that = this;
    return function(event){
        var offsetX, offsetY;
        var x, y;
        //window.getSelection.removeAllRanges();

        if (that.moveMouse){
            x = event.x;
            y = event.y;

            if (that.lastX != -1){
                offsetX = x - that.lastX;
            } else {
                offsetX = 0;
            }
            if (that.lastY != -1){
                offsetY = y - that.lastY;
            } else {
                offsetY = 0;
            }
            that.moveMouse(event.x, event.y, offsetX, offsetY);
            that.lastX = x;
            that.lastY = y;
        }
    };
}

// 各種のコールバック関数を登録する
// 外すときにはnullを渡す
MouseManager.prototype.setCallbackFunction = function(arg){
    //var default_val = {
    //    downLeft   : null,
    //    downCenter : null,
    //    downRight  : null,
    //    scrollWheel: null,
    //    moveMouse  : null
    //};

    if (arg.downLeft){
        this.downLeft = arg.downLeft;
    }
    if (arg.downCenter){
        this.downCenter = arg.downCenter;
    }
    if (arg.downRight){
        this.downRight = arg.downRight;
    }
    if (arg.scrollWheel){
        this.scrollWheel = arg.scrollWheel;
    }
    if (arg.moveMouse){
        this.moveMouse = arg.moveMouse;
    }
}

MouseManager.create = function(canvas){
    var mouseManager = new MouseManager();

    mouseManager.buttonPressed = [false, false, false];
    mouseManager.lastX = -1;
    mouseManager.lastY = -1;

    // canvasへmouse系のcallbackを登録
    canvas.onmouseover = mouseManager.overCallback();
    canvas.onmouseout = mouseManager.outCallback();
    canvas.onmouseup = mouseManager.upCallback();
    canvas.onmousedown = mouseManager.downCallback();
    canvas.onmousewheel = mouseManager.wheelCallback();
    canvas.onmousemove = mouseManager.moveCallback();

    return mouseManager;
}
